package com.ruoyi.accounting.controller;

import com.ruoyi.accounting.domain.AccClassification;
import com.ruoyi.accounting.domain.dto.AccClassificationDTO;
import com.ruoyi.accounting.service.AccClassificationService;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 分类Controller
 *
 * @author ruoyi
 * @date 2025-07-12
 */
@RestController
@RequestMapping("/classification")
public class AccClassificationController extends BaseController {
    @Autowired
    private AccClassificationService accClassificationService;

    /**
     * 查询分类列表
     */
    @RequiresPermissions("system:classification:list")
    @GetMapping("/list")
    public TableDataInfo list(AccClassification accClassification) {
        startPage();
        List<AccClassification> list = accClassificationService.selectAccClassificationList(accClassification);
        return getDataTable(list);
    }

    /**
     * 导出分类列表
     */
    @RequiresPermissions("system:classification:export")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccClassification accClassification) {
        List<AccClassification> list = accClassificationService.selectAccClassificationList(accClassification);
        ExcelUtil<AccClassification> util = new ExcelUtil<AccClassification>(AccClassification.class);
        util.exportExcel(response, list, "分类数据");
    }

    /**
     * 获取分类详细信息
     */
    @RequiresPermissions("system:classification:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(accClassificationService.selectAccClassificationById(id));
    }

    /**
     * 新增分类
     */
    @RequiresPermissions("system:classification:add")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid AccClassificationDTO accClassificationDTO) {
        AccClassification accClassification = new AccClassification();
        BeanUtils.copyBeanProp(accClassification, accClassificationDTO);
        return toAjax(accClassificationService.insertAccClassification(accClassification));
    }

    /**
     * 修改分类
     */
    @RequiresPermissions("system:classification:edit")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccClassification accClassification) {
        return toAjax(accClassificationService.updateAccClassification(accClassification));
    }

    /**
     * 删除分类
     */
    @RequiresPermissions("system:classification:remove")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(accClassificationService.deleteAccClassificationByIds(ids));
    }
}
